import {Component, OnInit, ViewChild, OnDestroy, ViewEncapsulation} from '@angular/core';
import {Subject} from "rxjs/Rx";
import {ParentService} from "@train/parent/parent.service";
import {ActivatedRoute, Params} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {TrainStudentTestScoreDetail} from "@interface/index";

@Component({
  selector: 'seed-history-scores',
  templateUrl: './history-scores.component.html',
  styleUrls: ['./history-scores.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class HistoryScoresComponent implements OnInit, OnDestroy {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public scoreList: Array<TrainStudentTestScoreDetail> = [];

  public params: any = {
    studentId: 7,
    levelId: 1,
    testModeId: 1,
    testMethodId: 1,
    page: 1,
    rows: 10
  };

  constructor(
    private tools: ToolsService,
    private parentService: ParentService,
    private route: ActivatedRoute,
  ) {
    this.route.params.switchMap((params: Params) => {
      this.params.studentId = params['studentId'];
      this.params.levelId = params['levelId'];
      this.params.testModeId = params['testModeId'];
      this.params.testMethodId = params['testMethodId'];
      this.getStudentScoreHistory();
      return this.route.queryParams;
    }).subscribe((queryParam: Params) => {
      this.params.testItemText = queryParam['testItemText'];
      this.params.ageGroupText = queryParam['ageGroupText'];
      this.params.testMethodName = queryParam['testMethodName'];
    });
  }

  /**
   * @description 查询学生历史成绩
   * @author @zhupw1360
   * @date 20180416
   */
  getStudentScoreHistory() {
    this.parentService.getStudentScoreHistory(this.params).subscribe((res) => {
      if (res.result === '0' && res.data.list.length > 0 ) {
        this.params['page']++;
        this.scoreList = [...this.scoreList, ...res.data.list];
        if (!res.data.hasNextPage) {
          this.infiniteScroll && this.infiniteScroll.enable(false);
        }
      }
    });
  }


  ngOnInit() {
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
